package com.szsh.aiot.hsm.controller;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.szsh.aiot.common.annotation.CustomPermission;
import com.szsh.aiot.common.annotation.Log;
import com.szsh.aiot.common.core.domain.AjaxResult;
import com.szsh.aiot.common.core.domain.RequestDto;
import com.szsh.aiot.common.enums.BusinessType;
import com.szsh.aiot.hsm.dto.enu.NumberEnum;
import com.szsh.aiot.hsm.service.NetworkProtectionService;
import com.szsh.aiot.hsm.utils.PermitValidUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collections;
import java.util.List;

/**
 * @Description:网络防护功能
 * @Date 2023/8/25: 9:16
 * @Author: LOADING>>>>>
 */

@Controller
@RequestMapping("/app/protect")
public class NetworkProtectionController {

    private static final Logger LOGGER = LoggerFactory.getLogger(NetworkProtectionController.class);

    @Autowired
    private PermitValidUtil permitValidUtil;

    @Autowired
    private NetworkProtectionService networkProtectionService;

    @Autowired
    private Gson gson;

    /**
     * 3.1 通过此接口进行网络防护开关状态查询以及修改开关状态
     * @param requestDto 请求参数
     * @return 处理结果
     */
    @RequestMapping(value = "/switch")
    @Log(title = "网络防护开关状态查询以及修改开关状态", businessType = BusinessType.OTHER)
    @CustomPermission(isValidateTyAccount = false)
    @ResponseBody
    public AjaxResult protectiveSwitchFunction(@RequestBody RequestDto requestDto){
        return businessProcessing(requestDto,NumberEnum.STR_1);
        }


    /**
     * 3.2 通过此接口完成用户告警绑定、解绑、修改、查询手机号码等功能
     * @param requestDto 请求参数
     * @return 处理结果
     */
    @RequestMapping(value = "/alertService")
    @Log(title = "短信通知", businessType = BusinessType.OTHER)
    @CustomPermission(isValidateTyAccount = false)
    @ResponseBody
    public AjaxResult messageAlarm(@RequestBody RequestDto requestDto){

        return businessProcessing(requestDto,NumberEnum.STR_2);
    }

    /**
     * 3.3 防护数据接口
     * @param requestDto 请求参数
     * @return 处理结果
     */
    @RequestMapping(value = "/protection/data")
    @Log(title = "防护数据", businessType = BusinessType.OTHER)
    @CustomPermission(isValidateTyAccount = false)
    @ResponseBody
    public AjaxResult protectionData(@RequestBody RequestDto requestDto){

        return businessProcessing(requestDto,NumberEnum.STR_3);
    }


    /**
     * 获取对应功能码
     * @param requestDto
     * @return
     */
    private AjaxResult businessProcessing(RequestDto requestDto,String parentSwitchValue){

        String sequenceNo = requestDto.getSequenceNo();
        JsonObject requestParams = requestDto.getRequestJsonElement().getAsJsonObject();
        String functionName = parentSwitchValue.equals("1")?"网络防护开关状态查询以及修改开关状态":
                parentSwitchValue.equals("2")?"用户告警绑定、解绑、修改、查询手机号码等功能":"防护数据";

        String operaType = permitValidUtil.getElementValue(requestParams,"operaType");
        LOGGER.info("sequenceNo:{},进行功能:{},的功能码:{}",sequenceNo,functionName,operaType);

        String broadbandNO = permitValidUtil.getElementValue(requestParams, "broadbandNO");
        String tyAccount = permitValidUtil.getElementValue(requestParams, "phone");

        switch (parentSwitchValue){
               //网络防护开关状态查询以及修改开关状态
            case  NumberEnum.STR_1:
                switch (operaType){
                    case  NumberEnum.STR_1:
                        return networkProtectionService.getSwithState(broadbandNO);
                    //查询
                    case  NumberEnum.STR_2:
                        String swithType = permitValidUtil.getElementValue(requestParams, "swithType");
                        String isOpen = permitValidUtil.getElementValue(requestParams, "isOpen");
                        return networkProtectionService.changeSwitchStatus(broadbandNO,swithType,isOpen);
                    //更新
                    default:
                        AjaxResult.badRequest("未知操作，请确认后重新操作");
                }

                //用户告警绑定、解绑、修改、查询手机号码等功能
            case  NumberEnum.STR_2:
                switch (operaType){
                    case  NumberEnum.STR_1:
                        return networkProtectionService.BindPhone(broadbandNO,tyAccount);
                    //绑定(新增)
                    case  NumberEnum.STR_2:
                        return networkProtectionService.unBindPhone(broadbandNO,tyAccount);
                    //解绑（删除）
                    case  NumberEnum.STR_3:
                        return networkProtectionService.getBindPhone(broadbandNO);
                        //查询
                    case  NumberEnum.STR_4:
                        return networkProtectionService.modBindPhone(broadbandNO,tyAccount);
                        //更新
                    default:
                        AjaxResult.badRequest("未知操作，请确认后重新操作");
                }

                // 防护数据接口
            case  NumberEnum.STR_3:
                String eventType = permitValidUtil.getElementValue(requestParams, "eventType");
                switch (operaType){
                    case  NumberEnum.STR_1:    //解绑（删除）
                        List<String> sernoList = gson.fromJson(requestParams.get("sernoList"),new TypeToken<List<String>>(){}.getType());
                       return networkProtectionService.delete(broadbandNO,eventType,sernoList);

                    case  NumberEnum.STR_2:
                        return networkProtectionService.getMaliciousInfo(broadbandNO,eventType);  //查询
                    default:
                       return AjaxResult.badRequest("未知操作，请确认后重新操作");
                }
            default:
              return   AjaxResult.badRequest("未知操作，请确认后重新操作");
        }
    }
}
